import { defineStore } from 'pinia'
import { reqLogin } from '@/api/user/index.ts'
import { SET_TOKEN, GET_TOKEN } from '@/utils/token.ts'
import type { loginFrom, loginResponseData } from '@/api/user/type.ts'
import type { UserState } from './types/type.ts'

// 引入路由（常量路由了）
import { constantRoute } from '@/routes/routes.ts'

const useUserStore = defineStore('User', {
  state: (): UserState => {
    return {
      token: GET_TOKEN(), // 用户唯一id
      menuRouters: constantRoute, // 仓库存储路由
    }
  },
  actions: {
    async userLogin(data: loginFrom) {
      const result: loginResponseData = await reqLogin(data)
      if (result.code === 200) {
        this.token = result.data.token as string
        SET_TOKEN(result.data.token as string)
        return 'ok'
      } else {
        return Promise.reject(new Error(result.data.message))
      }
    },
  },
  getters: {},
})
export default useUserStore
